package function

/**
 * Created by CJJ on 2017/7/14.
 *
 */

fun main(args: Array<String>) {

    //function reference
//
//    println(-5.5f % 4)
//
//
//    val funcRef: (Int) -> Boolean = ::isOdd
//
//
//    println(funcRef(2))
//    println(highOrderFunc(4, funcRef))
//    println(highOrderFunc(8, ::isOdd))


    //compose fun
//    var f = compose(::isOdd, ::parseInt)
//    println("100 is odd or not ?" + f("100"))
//
//    println(funWithDefaultValue(1, "ddddd"))

    var list = listOf( Person("alice"), Person("jack"))

    var sortedBy = list.sortedBy(::compFunc)
    println(sortedBy)
}

class Person  :Comparable<Boolean>{
    override fun compareTo(other: Boolean): Int {
        return name.toCharArray().size
    }

    var name=""

    constructor()
    constructor(name: String) {
        this.name = name
    }

    override fun toString(): String {
        return name
    }
}


fun compFunc(ins: Person): Int {
    return ins.hashCode()
}

//base =4
fun modeTailHeader(offset: Float): Int {

    val base = 4
    val ratio = offset % base
    val N = trunc(ratio)
    if (ratio < 0) {
        return base + N
    } else {
        return base - N
    }

}

fun trunc(i: Float): Int {

    val toInt = i.toInt()
    println(toInt)
    return toInt
}


fun highOrderFunc(value: Int, isOddFunc: (Int) -> Boolean): Boolean {
    return isOdd(value)
}

fun isOdd(value: Int): Boolean {
    return value % 2 == 0
}

fun isOdd(value: String): Boolean {
    return value.length % 2 == 0
}


//复杂组合,kotlin你为何要这么叼
fun compose(f: (Int) -> Boolean, g: (String) -> Int): (String) -> Boolean {
    //返回一个新的函数
    return {
        x ->
        f(g(x))
    }
}

// 默认值
fun funWithDefaultValue(a: Int = 0, b: String = "abc", c: Int = a + b.length): Int {
    return a + b.length
}


